<template>
	<view class="my-settle-container">
		<!-- 全选 -->
		<label class="radio" @click="radioChangeHandle">
			<radio color="#c00000" :checked="isFullChecked" /><text>全选</text>
		</label>
		
		<!-- 合计 -->
		<view class="amount-box">
			合计：<text class="amount">￥{{checkedCountPrice}}</text>
		</view>
		
		<!-- 结算按钮 -->
		<view class="btn-settle" @click="settlement">
			结算({{checkedCount}})
		</view>
	</view>
</template>

<script>
	import {mapGetters,mapMutations,mapState} from 'vuex'
	export default {
		name:"my-settle",
		computed:{
			// 利用辅助函数获取store中的getters
			...mapGetters('my_cart',['checkedCount','total','checkedCountPrice']),
			...mapGetters('my_user',['addressStr']),
			...mapState('my_user',['token']),
			...mapState('my_cart',['cart']),
			
			// 判断是否全选
			isFullChecked(){
				return this.total===this.checkedCount
			}
		},
		data() {
			return {
				// 定时器秒数
				seconds:3,
				// 定时器
				timer:null
			};
		},
		methods:{
			...mapMutations('my_cart',['updateRadioChangeHandle']),
			...mapMutations('my_user',['updateRedirctInfo']),
			// 改变全选框的状态
			radioChangeHandle(){
				this.updateRadioChangeHandle(!this.isFullChecked)
			},
			// 用户点击结算
			settlement(){
				if(!this.addressStr) return uni.$showMsg('请选择收货地址')
				if(!this.checkedCount) return uni.$showMsg('请选择要购买的商品')
				if(JSON.stringify(this.token)==='{}'){
					this.delayNavigate()
				}
				this.payOrder()
			},
			// 展示倒计时
			showTips(n){
				uni.showToast({
					icon:'icon',
					title:n+'秒后跳转登录',
					mask:true,
					duration:1500
				})
			},
			// 延迟导航到登录页面
			delayNavigate(){
				this.seconds=3
				this.showTips(this.seconds)
				this.timer = setInterval(()=>{
					this.seconds--
					if(this.seconds<=0){
						clearInterval(this.timer)
						uni.switchTab({
							url:'/pages/my/my',
							success:()=>{
								this.updateRedirctInfo({
									openType:'switchTab',
									from:'/pages/cart/cart'
								})
							}
						})
						return
					}
					this.showTips(this.seconds)
				},1000)
			},
			// 创建订单
			async payOrder(){
				// 订单信息对象
				const orderInfo={
					order_price:this.checkedCountPrice,
					consignee_addr:this.addressStr,
					goods:this.cart.filter(x=>x.goods_state).map(x=>({
						goods_id:x.goods_id,
						goods_number:x.goods_count,
						goods_price:x.goods_pricedata
					}))
				}
				//订单生成订单
				const {data} = await uni.$http.post('/api/public/v1/my/orders/create',orderInfo)
				if(data.meta.status!==200) return uni.$showMsg('创建订单失败')
				const orderNumber = data.message.order_number
				// 订单发起预定单
				const {data:res2} = await uni.$http.post('/api/public/v1/my/orders/req_unifiedorder',{order_number:orderNumber})
				if(res2.meta.status!==200) return uni.$showMsg('预定单生成失败')
				const payInfo = res2.message.pay
				// 发起微信支付
				const res3 = await uni.requestPayment(payInfo)
				console.log(res3);
				// 检测订单支付状态
				const {data:res4} = await uni.$http.post('/api/public/v1/my/orders/chkOrder',{order_number:orderNumber})
				if(res4.meta.status!==200) return uni.$showMsg('订单未支付')
				// 订单支付完成
				uni.showToast({
					title:'订单支付完成',
					icon:'success'
				})
				
			}
		}
	}
</script>

<style lang="scss">
	.my-settle-container{
		position: fixed;
		bottom: 0;
		left: 0;
		height: 50px;
		width: 100%;
		display: flex;
		justify-content: space-between;
		align-items: center;
		font-size: 14px;
		padding-left: 5px;
		.radio{
			display:flex;
			align-items: center;
		}
		.amount-box{
			.amount{
				color: #c00000;
				font-weight: bold;
			}
		}
		.btn-settle{
			background-color: #c00000;
			height: 50px;
			color: white;
			line-height: 50px;
			padding: 0 10px;
			min-width: 100px;
			text-align: center;
		}
	}
</style>